package top.lishuoboy.hibernate.validator.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.lishuoboy.hibernate.validator.dto.PageReq;
import top.lishuoboy.hibernate.validator.dto.Result;
import top.lishuoboy.hibernate.validator.mapper.SysUserMapper;
import top.lishuoboy.hibernate.validator.model.SysUser;
import top.lishuoboy.hibernate.validator.service.SysUserService;
import top.lishuoboy.hibernate.validator.validate.group.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import java.util.List;

/**
 * @author lishuoboy
 * @date 2024/10/24
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@ApiSupport(order = 10, author = "李硕")
@Api(tags = "用户-sys-user-controller")
@RequestMapping("/SysUser")
//@NotResult
@Validated
public class SysUserController {
    private final SysUserService sysUserService;
    private final SysUserMapper sysUserMapper;

    /** bean校验-Controller类-分组校验-Insert组 */
    @ApiOperationSupport(order = 11/*, author = "小石头"*/)
    @ApiOperation(value = "保存-save", notes = "保存-notes")
    @PostMapping("/save")
    private Result<SysUser> save(@RequestBody @Validated({Insert.class, Default.class})  /*@Valid*/ SysUser sysUser) {
        sysUserService.save(sysUser);
        return Result.success(sysUser);
    }

    /** bean校验-Controller类-分组校验-DeleteById组 */
    @ApiOperationSupport(order = 21, author = "小石头", includeParameters = {"sysUser.userId"})
    @ApiOperation(value = "删除-removeById", notes = "删除-notes")
    @PostMapping("/removeById")
    Result<Boolean> removeById(@RequestBody @Validated({DeleteById.class, Default.class}) /*@Valid*/ SysUser sysUser) {
        return Result.success(sysUserService.removeById(sysUser));
    }

    /** bean校验-Controller类-分组校验-UpdateById组 */
    @ApiOperationSupport(order = 31, author = "小石头")
    @ApiOperation(value = "更新-updateById", notes = "更新-notes")
    @PostMapping("/updateById")
    Result<SysUser> updateById(@RequestBody @Validated({UpdateById.class, Default.class})  /*@Valid*/ SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return Result.success(sysUser);
    }

    @ApiOperationSupport(order = 41, author = "小石头", includeParameters = {"sysUser.userId"})
    @ApiOperation(value = "详情-getById", notes = "详情-notes")
    @PostMapping("/getById")
    Result<SysUser> getById(@RequestBody @Validated({SelectById.class, Default.class}) SysUser sysUser) {
        return Result.success(sysUserService.getById(sysUser.getUserId()));
    }

    /** 参数校验-Controller类 */
    @ApiOperationSupport(order = 42, author = "小石头")
    @ApiOperation(value = "详情-getById_param", notes = "详情-notes")
    @PostMapping("/getById_param")
    Result<SysUser> getById_param(@RequestParam @NotNull @Min(0) Integer id) {
        return Result.success(sysUserService.getById(id));
    }

    @ApiOperationSupport(order = 51, author = "小石头")
    @ApiOperation(value = "分页-page", notes = "返回Page对象-notes")
    @PostMapping("/page")
    Result<Page<SysUser>> page(@RequestBody @Validated({Select.class, Default.class}) PageReq<SysUser> pageReq) {
//        System.out.println(2/0);
        return Result.success(sysUserService.page(pageReq.toPage(), new LambdaQueryWrapper<>(pageReq.getCondition())));
    }

    @ApiOperationSupport(order = 61, author = "小石头")
    @ApiOperation(value = "列表-list", notes = "返回List对象-notes")
    @PostMapping("/list")
//    @NotResult
    List<SysUser> list(@RequestBody @Validated({Select.class, Default.class}) PageReq<SysUser> pageReq) {
        return sysUserService.list(pageReq.toPage(), new LambdaQueryWrapper<>(pageReq.getCondition()));
    }


    /** bean校验-非Controller类-分组校验-Insert组 */
    @ApiOperationSupport(order = 111/*, author = "小石头"*/)
    @ApiOperation(value = "保存-save2", notes = "保存-notes")
    @PostMapping("/save2")
    Result<SysUser> save2(@RequestBody SysUser sysUser) {
        sysUserService.save2(sysUser);
        return Result.success(sysUser);
    }

    /** bean校验-非Controller类-分组校验-UpdateById组 */
    @ApiOperationSupport(order = 131, author = "小石头")
    @ApiOperation(value = "更新-updateById2", notes = "更新-notes")
    @PostMapping("/updateById2")
    Result<SysUser> updateById2(@RequestBody SysUser sysUser) {
        sysUserService.updateById2(sysUser);
        return Result.success(sysUser);
    }

    /** 参数校验-非Controller类 */
    @ApiOperationSupport(order = 141, author = "小石头")
    @ApiOperation(value = "详情-getById2", notes = "详情-notes")
    @PostMapping("/getById2")
    Result<SysUser> getById2(@RequestParam Integer id) {
        return Result.success(sysUserService.getById2(id));
    }

}
